package com.example.yk_lx.mine;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.CycleInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.example.yk_lx.R;
import com.hyphenate.chat.EMClient;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;


public class MineFragment extends Fragment {
    private ImageView iv;
    private Button ercode;
    private Button updateIv;
    private TextView tv;
    private Button logout;







    public MineFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_mine, container, false);
        iv = (ImageView) inflate.findViewById(R.id.iv);
        ercode = (Button) inflate.findViewById(R.id.ercode);
        updateIv = (Button) inflate.findViewById(R.id.updateIv);
        tv = (TextView) inflate.findViewById(R.id.tv);
        logout = (Button) inflate.findViewById(R.id.logout);
        //生成二维码
        ercode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_money);
                Bitmap image = CodeUtils.createImage(SPUtils.getInstance().getString("username"), 200, 200, bitmap);
                iv.setImageBitmap(image);
            }
        });

        //修改头像
        updateIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PictureSelector.create(getActivity(),200).selectPicture();//打开相册，展示图片
            }
        });
        //点击头像，添加动画
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivAnimator();
            }
        });

        tv.setText(SPUtils.getInstance().getString("name"));
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("aaaa");
                SPUtils.getInstance().put("name",tv.getText().toString());
            }
        });
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EMClient.getInstance().logout(true);
                SPUtils.getInstance().clear();
                ARouter.getInstance().build("/app/LoginActivity").navigation();
            }
        });
        return inflate;
    }

    private void ivAnimator() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "rotation", 0, 720);//旋转
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 1, 2, 1);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 1, 2, 1);//放大1倍再恢复
        ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 1, 0, 1);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(animator).with(scaleX).with(scaleY).with(alpha);
        animatorSet.setDuration(2000);
        animatorSet.start();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==200&&resultCode== Activity.RESULT_OK){
            PictureBean o = (PictureBean) data.getExtras().get(PictureSelector.PICTURE_RESULT);
            Glide.with(getContext()).load(o.getPath()).into(iv);
        }
    }
}